package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.*;
import com.mycom.service.EmployeeService;
import com.mycom.service.SubmitFormService;
import com.mycom.service.SubmitItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/submitForms")
public class SubmitFormController {
    @Autowired
    private SubmitFormService submitFormService;
    @Autowired
    private SubmitItemService submitItemService;
    @Autowired
    private EmployeeService employeeService;

    @PostMapping
    public Result save(@RequestBody SubmitForm submitForm){
        Employee employee = employeeService.findByIdCard(submitForm.getIdCardNumber());
        String formNumber = employee.getDepartmentId() + "-" + employee.getId() + "-" + submitForm.getSubmitDate().getTime();
        submitForm.setFormNumber(formNumber);
        //临时指定审核人，后期完成权限管理再修改
        submitForm.setReviewer("林生");
        submitForm.setReviewerIdCardNumber("440882195412204773");
        boolean flag1 = submitFormService.save(submitForm);
        List<SubmitItem> submitItems = submitForm.getSubmitItems();
        boolean flag2 = true;
        for (SubmitItem submitItem : submitItems) {
            submitItem.setFormId(submitForm.getId());
            boolean flag3 = submitItemService.save(submitItem);
            flag2 = flag2 & flag3;
        }
        boolean flag = flag1 & flag2;
        return new Result(flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR,flag);
    }

    @GetMapping("/{idCardNumber}")
    public Result findByIdCard(@PathVariable String idCardNumber){
        List<SubmitForm> submitForms = submitFormService.findByIdCardNumber(idCardNumber);
        for (SubmitForm submitForm : submitForms) {
            List<SubmitItem> submitItems = submitItemService.findByFormId(submitForm.getId());
            submitForm.setSubmitItems(submitItems);
        }
        Integer code = submitForms!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = submitForms!=null ? "查询待审核报表成功！" : "查询待审核报表失败！";
        return new Result(code,submitForms,msg);
    }

    /**
     * 将待提交的报表保存到待审核
     * @param userForm
     * @return
     */
    @RequestMapping("/saveWaitSubmit")
    public Result saveWaitSubmit(@RequestBody UserForm userForm){
        List<FormItem> formItems = userForm.getFormItems();
        List<SubmitItem> submitItems = new ArrayList<>();
        int i = 0;
        for (FormItem formItem : formItems) {
            SubmitItem submitItem = new SubmitItem(formItem.getFormId(),formItem.getItem(),
                    formItem.getGrade(),formItem.getNumber(),formItem.getCount(),formItem.getRemark());
            submitItems.add(submitItem);
        }
        //审核人：由填报人所属部门领导审核
        String reviewer = "林生";
        String reviewerIdCardNumber = "440882195412204773";
        //报表编号
        Employee employee = employeeService.findByIdCard(userForm.getIdCardNumber());
        String formNumber = employee.getDepartmentId() + "-" + employee.getId() + "-" + userForm.getSaveDate().getTime();
        SubmitForm submitForm = new SubmitForm(userForm.getIdCardNumber(),formNumber,userForm.getWorkDate(),
                userForm.getSaveDate(),reviewer,reviewerIdCardNumber,submitItems);
        boolean flag1 = submitFormService.save(submitForm);
        boolean flag2 = true;
        for (SubmitItem submitItem : submitItems) {
            submitItem.setFormId(submitForm.getId());
            boolean flag0 = submitItemService.save(submitItem);
            flag2 = flag2 & flag0;
        }
        boolean flag = flag1 & flag2;
        Integer code = flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR;
        String msg = flag ? "提交报表成功！" : "提交报表失败！";
        return new Result(code,flag,msg);
    }
    //查询待审核
    @RequestMapping("/findPersonWaitReview")
    public Result findByReviewerIdCard(String idCardNumber){
        List<SubmitForm> submitForms = submitFormService.findByReviewerIdCardNumber(idCardNumber);
        for (SubmitForm submitForm : submitForms) {
            Integer formId = submitForm.getId();
            List<SubmitItem> submitItems = submitItemService.findByFormId(formId);
            submitForm.setSubmitItems(submitItems);
        }
        Integer code = submitForms!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = submitForms!=null ? "查询个人待审核报表成功！":"查询个人待审核报表失败！";
        return new Result(code,submitForms,msg);
    }
}
